﻿using System;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using WCSY.BLL;
using Winsteps.Validator;
using WCSY.MODEL;

public partial class login : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack){
            WinVal val = new WinVal();
            val.ValStyle = "toppoptip";
            val.SetValidator();
        }
        

    }
    protected void IbtnEnter_Click(object sender, ImageClickEventArgs e)
    {
        if(Page.IsValid){
            Mg_userManager userM = new Mg_userManager();
            string userid = this.TxtUserName.Text.Trim();
            string password = this.TxtPassword.Text.Trim();
            string url = Request.Url.LocalPath;
            mg_user user = userM.GetModel(userid);
            if (user != null)
            {
                if (!user.passWord.Equals(password))
                {

                    Response.Write("<script>alert('密码错误，请确认后重新输入！');</script>");
                    tool.Redirect(url);

                }
                else if (user.lgSign == 0)
                {
                    Response.Write("<script>alert('当前用户已禁用！');</script>");
                    tool.Redirect(url);
                }
                else if (user.status == 1)
                {
                    Response.Write("<script>alert('当前用户已登陆！')</script>");
                    tool.Redirect(url);
                }
                else
                {
                    if (user.userName.Equals("administrator") || user.userName.Equals("Administrator") || user.userName.Equals("ADMINISTRATOR"))
                    {
                        Session.Add("userId", "administrator");
                        Session.Add("userName", user.userName);
                        Session.Add("userSysId", user.id);
                        //tool.Redirect("tempalette/Main.aspx");
                        tool.Redirect("Default.aspx");

                    }
                    else
                    {
                        Session.Add("userId", user.userId);
                        Session.Add("userName", user.userName);
                        Session.Add("userSysId",user.id);
                        userM.ModifyMg_userStatus(user.userId.ToString(),"1");
                        tool.Redirect("Default.aspx");
                    }

                }

            }
            else {
                Response.Write("<script>alert('当前用户不存在！');</script>");
                tool.Redirect(url);
            
            }
        
        }
    }
}
